package com.example.administrator.chesstest01;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements View.OnClickListener{

//    在AndroidManifest.xml的activity中加入:
//    android:screenOrientation="landscape"
//    属性即可(landscape是横向，portrait是纵向)。
    @BindView(R.id.btn_create) Button btnCreate;
    @BindView(R.id.btn_join) Button btnJoin;
    @BindView(R.id.et_room_name) EditText etRoomName;
    @BindView(R.id.et_user_name) EditText etUserName;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Common.JsonCmdResp.CREATE_ROOM:
                    try {
                        JSONObject jo = new JSONObject(msg.obj.toString());
                        int status = jo.getInt("status");
                        if (status == 0){
                            room_id = AnalysisJson.ExitRoomId(msg.obj.toString(),room_id);
                            Intent intentChess = new Intent(MainActivity.this,ChessRoomActivity.class);
                            intentChess.putExtra("room_id",room_id);
                            startActivity(intentChess);
                        }
                        else{Toast.makeText(MainActivity.this, "创建房间失败", Toast.LENGTH_SHORT).show();}
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                default:
                    break;
            }
        }
    };
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获得service中binder对象
            binder = (MyService.OptBinder) service;
            binder.setHandler(handler);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            binder = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Intent intent = new Intent(this,MyService.class);
        bindService(intent,conn, Service.BIND_AUTO_CREATE);
        btnCreate.setOnClickListener(this);
        btnJoin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_create:
                if (!etRoomName.getText().toString().equals("")){
                    AnalysisJson.CreateRoom(etRoomName,binder);
                }else{Toast.makeText(MainActivity.this, "请输入房间名", Toast.LENGTH_SHORT).show();}
                break;
            case R.id.btn_join:
                Intent intent2 = new Intent(MainActivity.this,UsersListActivity.class);
                startActivity(intent2);
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //activity消亡解除绑定
        if (null != conn){
            unbindService(conn);
        }
    }

}
